<!DOCTYPE html>
<html lang="cn-zh">

<head>
  

  
  


  <meta charset="utf-8">



<meta name="viewport" content="width=device-width, initial-scale=1.0">


  
    
      <meta name="description" content="开发的乐趣在于相通而不同">
    
  


<meta name="color-scheme" content="light dark">







<meta name="generator" content="Hugo 0.111.2">

<meta content="" name="keywords">
<meta content="通通不同-博客" property="og:title">
<meta content="开发的乐趣在于相通而不同" property="og:description">

<meta name="msvalidate.01" content="F56ED94B7E86941E3DACEF208F07D329" />

  <title>
    
    通通不同-博客
    
</title>

  <link rel="canonical" href="https://codebeautiful.gitee.io/">


  <link rel="alternate" type="application/rss+xml" href="/index.xml" title="通通不同-博客">

  








  

  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
    
    
  
    
    
    
      
      
    
    
    
  
    
    
    
      
      
    
    
    
  


  
  <link rel="stylesheet" href="/css/base.min.4f231885947d13c748c815d93da49f07ab89afb67629f9970b7c054610e88e0b.css" integrity="sha256-TyMYhZR9E8dIyBXZPaSfB6uJr7Z2KfmXC3wFRhDojgs=" crossorigin="anonymous">



</head>

<body>
  <nav class="u-background">
  <div class="u-wrapper">
    <ul class="Banner">
      <li class="Banner-item Banner-item--title">
        <h1 class="Banner-heading">
          <a class="Banner-link u-clickable" href="/">通通不同-博客</a>
        </h1>
      </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/post/">全部</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/categories/">分类</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/tags/">标签</a>
        </li>
      
        <li class="Banner-item">
          <a class="Banner-link u-clickable" href="/about/">关于</a>
        </li>
      
    </ul>
  </div>
</nav>

  <main>
    <div class="u-wrapper">
      <div class="u-padding">
        

  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/android_gradle_could_not_resolve/" rel="bookmark">Android 新建工程报错 Could not resolve com.android.tools.build:gradle:8.0.1.</a>
  </h2>
  
    <time datetime="2023-05-07T15:12:30&#43;08:00">7 May, 2023</time>
  
</header>

      
        <p>解决方法修改Gradle JDK版本</p>
<p>File &gt; Settings &gt; Build, Execution, Deployment &gt; build tools &gt; gradle Then Change Gradle JDK from the dropdown and select Android Studio java home</p>

      
      


  

  

  





    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/spring_boot_scheduled/" rel="bookmark">Spring boot 实现定时任务</a>
  </h2>
  
    <time datetime="2023-05-06T11:57:47&#43;08:00">6 May, 2023</time>
  
</header>

      
        <ol>
<li>主类添加<code>@EnableScheduling</code>注解</li>
<li>实现类添加<code>@Component</code></li>
<li>具体方法添加<code>@Scheduled</code></li>
</ol>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-java" data-lang="java"><span style="display:flex;"><span><span style="color:#a6e22e">@EnableScheduling</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@SpringBootApplication</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">SpringBootApplication</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">static</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">main</span><span style="color:#f92672">(</span>String<span style="color:#f92672">[]</span> args<span style="color:#f92672">)</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>        SpringApplication<span style="color:#f92672">.</span><span style="color:#a6e22e">run</span><span style="color:#f92672">(</span>SpringBootApplication<span style="color:#f92672">.</span><span style="color:#a6e22e">class</span><span style="color:#f92672">);</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">@Component</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">public</span> <span style="color:#66d9ef">class</span> <span style="color:#a6e22e">ScheduledTask</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>    <span style="color:#a6e22e">@Scheduled</span><span style="color:#f92672">(</span>cron <span style="color:#f92672">=</span> <span style="color:#e6db74">&#34;0 * * * * ?&#34;</span><span style="color:#f92672">)</span>
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">public</span> <span style="color:#66d9ef">void</span> <span style="color:#a6e22e">run</span><span style="color:#f92672">()</span> <span style="color:#f92672">{</span>
</span></span><span style="display:flex;"><span>        System<span style="color:#f92672">.</span><span style="color:#a6e22e">out</span><span style="color:#f92672">.</span><span style="color:#a6e22e">println</span><span style="color:#f92672">(</span><span style="color:#e6db74">&#34;现在时间：&#34;</span> <span style="color:#f92672">+</span> dateFormat<span style="color:#f92672">.</span><span style="color:#a6e22e">format</span><span style="color:#f92672">(</span><span style="color:#66d9ef">new</span> Date<span style="color:#f92672">()));</span>
</span></span><span style="display:flex;"><span>    <span style="color:#f92672">}</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">}</span></span></span></code></pre></div>
<p><a href="https://www.bejson.com/othertools/cron/">Cron表达式在线生成工具</a></p>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/springboot/" rel="tag">SpringBoot</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%AE%9A%E6%97%B6%E4%BB%BB%E5%8A%A1/" rel="tag">定时任务</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/golang_parse_sitemap_submit_baidu/" rel="bookmark">Golang_解析hugo生成的sitemap提交百度搜索</a>
  </h2>
  
    <time datetime="2023-05-06T11:00:10&#43;08:00">6 May, 2023</time>
  
</header>

      
        
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">import</span> (
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;encoding/hex&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;encoding/xml&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;fmt&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;os&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;strings&#34;</span>
</span></span><span style="display:flex;"><span>	<span style="color:#e6db74">&#34;testing&#34;</span>
</span></span><span style="display:flex;"><span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Node</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Loc</span> <span style="color:#66d9ef">string</span> <span style="color:#e6db74">`xml:&#34;loc&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Root</span> <span style="color:#66d9ef">struct</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">XMLName</span> <span style="color:#a6e22e">xml</span>.<span style="color:#a6e22e">Name</span> <span style="color:#e6db74">`xml:&#34;urlset&#34;`</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">Nodes</span>   []<span style="color:#a6e22e">Node</span>   <span style="color:#e6db74">`xml:&#34;url&#34;`</span>
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">file</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">Open</span>(<span style="color:#e6db74">&#34;../public/sitemap.xml&#34;</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		panic(<span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">Close</span>()
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">var</span> <span style="color:#a6e22e">root</span> <span style="color:#a6e22e">Root</span>
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">decoder</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">xml</span>.<span style="color:#a6e22e">NewDecoder</span>(<span style="color:#a6e22e">file</span>)
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">decoder</span>.<span style="color:#a6e22e">Decode</span>(<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">root</span>); <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>		panic(<span style="color:#a6e22e">err</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">node</span> <span style="color:#f92672">:=</span> <span style="color:#66d9ef">range</span> <span style="color:#a6e22e">root</span>.<span style="color:#a6e22e">Nodes</span> {
</span></span><span style="display:flex;"><span>		<span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">node</span>.<span style="color:#a6e22e">Loc</span>)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
<p>以下是sitemap.xml的文件获取路径</p>
<p><a href="https://codebeautiful.gitee.io/sitemap.xml">https://codebeautiful.gitee.io/sitemap.xml</a></p>

      
      


  

  

  





    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/qt_https_failed_to_load_libssl_libcrypto/" rel="bookmark">Qt开发https请求报 qt.tlsbackend.ossl: Failed to load libssl/libcrypto.</a>
  </h2>
  
    <time datetime="2023-05-06T09:36:26&#43;08:00">6 May, 2023</time>
  
</header>

      
        <p>使用环境是 macOS13  Qt Creator版本是9.0</p>
<p>错误原因分析：未导入 ssl 和 crypto 相关库</p>
<p>解决方法：在 .pro 文件中加入一下代码</p>

<pre tabindex="0"><code>LIBS += -lssl
LIBS += -lcrypto

LIBS    += -L&#34;/opt/homebrew/Cellar/openssl@1.1/1.1.1s/lib&#34;
INCLUDEPATH += /opt/homebrew/Cellar/openssl@1.1/1.1.1s/include</code></pre>
<p>其中openssl的路径依据安装路径不同存在不一致的情况，我自己的openssl是通过  brew install openssl  如果是M系列可能需要使用  arch -arm64 brew install openssl</p>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/qt/" rel="tag">Qt</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/ssl/" rel="tag">ssl</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/https/" rel="tag">https</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/crypto/" rel="tag">crypto</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/brew/" rel="tag">brew</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/openssl/" rel="tag">openssl</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/swift_notification_usage/" rel="bookmark">Swift Notification使用</a>
  </h2>
  
    <time datetime="2023-05-05T15:46:32&#43;08:00">5 May, 2023</time>
  
</header>

      
        <h3 id="简单使用记录">
  <a class="Heading-link u-clickable" href="/post/swift_notification_usage/#%e7%ae%80%e5%8d%95%e4%bd%bf%e7%94%a8%e8%ae%b0%e5%bd%95">简单使用记录</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-swift" data-lang="swift"><span style="display:flex;"><span><span style="color:#75715e">//注册监听</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">var</span> observer = NotificationCenter.<span style="color:#66d9ef">default</span>.addObserver(forName: Notification.Name(rawValue: <span style="color:#e6db74">&#34;Notify&#34;</span>), object: <span style="color:#66d9ef">nil</span>, queue: OperationQueue.main) {<span style="color:#66d9ef">_</span> <span style="color:#66d9ef">in</span>
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span><span style="color:#75715e">//移除监听</span>
</span></span><span style="display:flex;"><span>NotificationCenter.<span style="color:#66d9ef">default</span>.removeObserver(observer)
</span></span><span style="display:flex;"><span><span style="color:#75715e">//发送通知</span>
</span></span><span style="display:flex;"><span>NotificationCenter.<span style="color:#66d9ef">default</span>.post(name: Notification.Name(<span style="color:#e6db74">&#34;Notify&#34;</span>), object: <span style="color:#66d9ef">nil</span>)</span></span></code></pre></div>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/swift/" rel="tag">swift</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/notification/" rel="tag">Notification</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/ios_upload_ipa_solve_bluetooth_permission/" rel="bookmark">iOS上架ipa，解决蓝牙权限申请问题</a>
  </h2>
  
    <time datetime="2023-04-25T21:01:58&#43;08:00">25 April, 2023</time>
  
</header>

      
        <h3 id="收到错误的邮件信息">
  <a class="Heading-link u-clickable" href="/post/ios_upload_ipa_solve_bluetooth_permission/#%e6%94%b6%e5%88%b0%e9%94%99%e8%af%af%e7%9a%84%e9%82%ae%e4%bb%b6%e4%bf%a1%e6%81%af">收到错误的邮件信息</a>
</h3>
<p><strong>ITMS-90683: Missing purpose string in Info.plist</strong> - Your app’s code references one or more APIs that access sensitive user data, or the app has one or more entitlements that permit such access. The Info.plist file for the “iosApp.app” bundle should contain a <em>NSBluetoothPeripheralUsageDescription</em> key with a user-facing purpose string explaining clearly and completely why your app needs the data. If you’re using external libraries or SDKs, they may reference APIs that require a purpose string. While your app might not use these APIs, a purpose string is still required. For details, visit:</p>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-xml" data-lang="xml"><span style="display:flex;"><span><span style="color:#f92672">&lt;key&gt;</span>NSBluetoothPeripheralUsageDescription<span style="color:#f92672">&lt;/key&gt;</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&lt;string&gt;</span>Bluetooth is used to communicate with BLE devices.<span style="color:#f92672">&lt;/string&gt;</span></span></span></code></pre></div>
<p><img src="/images/image-20230425210600856.png" alt="image-20230425210600856"></p>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/ios/" rel="tag">iOS</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/nsbluetoothperipheralusagedescription/" rel="tag">NSBluetoothPeripheralUsageDescription</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E4%B8%8A%E6%9E%B6app/" rel="tag">上架APP</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E8%93%9D%E7%89%99/" rel="tag">蓝牙</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/ios_viewdiddisappear_without_destroying_objects/" rel="bookmark">iOS navigationController 跳转页面不销毁对象，退出销毁对象</a>
  </h2>
  
    <time datetime="2023-04-21T11:49:45&#43;08:00">21 April, 2023</time>
  
</header>

      
        <h3 id="需求分析">
  <a class="Heading-link u-clickable" href="/post/ios_viewdiddisappear_without_destroying_objects/#%e9%9c%80%e6%b1%82%e5%88%86%e6%9e%90">需求分析</a>
</h3>
<ol>
<li>进入界面开启蓝牙连接</li>
<li>中间有跳转其他页面，需要再其他页面保持蓝牙连接状态</li>
<li>退出界面后需要断开蓝牙连接</li>
</ol>
<h3 id="解决办法">
  <a class="Heading-link u-clickable" href="/post/ios_viewdiddisappear_without_destroying_objects/#%e8%a7%a3%e5%86%b3%e5%8a%9e%e6%b3%95">解决办法</a>
</h3>
<p>在viewDidDisappear中处理连接断开问题</p>
<p>在该方法中，如果是跳转界面self.navigationController不为空，如果退出当前界面得到的值为nil</p>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-swift" data-lang="swift"><span style="display:flex;"><span><span style="color:#66d9ef">override</span> <span style="color:#66d9ef">func</span> <span style="color:#a6e22e">viewDidDisappear</span>(<span style="color:#66d9ef">_</span> animated: Bool) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span> <span style="color:#66d9ef">self</span>.navigationController == <span style="color:#66d9ef">nil</span> {
</span></span><span style="display:flex;"><span>    		<span style="color:#75715e">// todo 调用蓝牙断开方法</span>
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/ios/" rel="tag">iOS</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/uinavigationcontroller/" rel="tag">UINavigationController</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%AF%B9%E8%B1%A1%E9%94%80%E6%AF%81/" rel="tag">对象销毁</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%AF%B9%E8%B1%A1%E4%BF%9D%E7%95%99/" rel="tag">对象保留</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E8%93%9D%E7%89%99/" rel="tag">蓝牙</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/xiaomi_xmodem_crc32_crc16_crc8/" rel="bookmark">小米xmodem协议crc32 crc16 crc8 使用</a>
  </h2>
  
    <time datetime="2023-04-21T10:04:27&#43;08:00">21 April, 2023</time>
  
</header>

      
        <h3 id="crc8校验">
  <a class="Heading-link u-clickable" href="/post/xiaomi_xmodem_crc32_crc16_crc8/#crc8%e6%a0%a1%e9%aa%8c">crc8校验</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdint.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">uint8_t</span> <span style="color:#a6e22e">crc8</span>(<span style="color:#66d9ef">uint8_t</span> <span style="color:#f92672">*</span>p_buffer, <span style="color:#66d9ef">uint16_t</span> buf_size);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">uint8_t</span> <span style="color:#a6e22e">crc8</span>(<span style="color:#66d9ef">uint8_t</span> <span style="color:#f92672">*</span>p_buffer, <span style="color:#66d9ef">uint16_t</span> buf_size)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">uint8_t</span> crc <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">uint8_t</span> i<span style="color:#f92672">=</span><span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">if</span>(buf_size <span style="color:#f92672">&lt;=</span> <span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">return</span> crc;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">while</span>( buf_size<span style="color:#f92672">--</span> )
</span></span><span style="display:flex;"><span>    {
</span></span><span style="display:flex;"><span>        <span style="color:#66d9ef">for</span> (  i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0x80</span>; i <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span>; i <span style="color:#f92672">/=</span> <span style="color:#ae81ff">2</span> )
</span></span><span style="display:flex;"><span>        {
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span> ( (crc <span style="color:#f92672">&amp;</span> <span style="color:#ae81ff">0x80</span>) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>            {
</span></span><span style="display:flex;"><span>                crc <span style="color:#f92672">*=</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>                crc <span style="color:#f92672">^=</span> <span style="color:#ae81ff">0x07</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">else</span>
</span></span><span style="display:flex;"><span>            {
</span></span><span style="display:flex;"><span>                crc <span style="color:#f92672">*=</span> <span style="color:#ae81ff">2</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>            <span style="color:#66d9ef">if</span> ( (<span style="color:#f92672">*</span>p_buffer <span style="color:#f92672">&amp;</span> i) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span> )
</span></span><span style="display:flex;"><span>            {
</span></span><span style="display:flex;"><span>                crc <span style="color:#f92672">^=</span> <span style="color:#ae81ff">0x07</span>;
</span></span><span style="display:flex;"><span>            }
</span></span><span style="display:flex;"><span>        }
</span></span><span style="display:flex;"><span>        p_buffer<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">return</span> crc;
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
<h3 id="crc16">
  <a class="Heading-link u-clickable" href="/post/xiaomi_xmodem_crc32_crc16_crc8/#crc16">crc16</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">/* CRC16 implementation acording to CCITT standards */</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">short</span> <span style="color:#a6e22e">crc16_ccitt_check</span>(<span style="color:#66d9ef">const</span> <span style="color:#66d9ef">void</span> <span style="color:#f92672">*</span>buf, <span style="color:#66d9ef">int</span> len);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">static</span> <span style="color:#66d9ef">const</span> <span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">short</span> crc16tab[<span style="color:#ae81ff">256</span>]<span style="color:#f92672">=</span> {
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x0000</span>,<span style="color:#ae81ff">0x1021</span>,<span style="color:#ae81ff">0x2042</span>,<span style="color:#ae81ff">0x3063</span>,<span style="color:#ae81ff">0x4084</span>,<span style="color:#ae81ff">0x50a5</span>,<span style="color:#ae81ff">0x60c6</span>,<span style="color:#ae81ff">0x70e7</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x8108</span>,<span style="color:#ae81ff">0x9129</span>,<span style="color:#ae81ff">0xa14a</span>,<span style="color:#ae81ff">0xb16b</span>,<span style="color:#ae81ff">0xc18c</span>,<span style="color:#ae81ff">0xd1ad</span>,<span style="color:#ae81ff">0xe1ce</span>,<span style="color:#ae81ff">0xf1ef</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x1231</span>,<span style="color:#ae81ff">0x0210</span>,<span style="color:#ae81ff">0x3273</span>,<span style="color:#ae81ff">0x2252</span>,<span style="color:#ae81ff">0x52b5</span>,<span style="color:#ae81ff">0x4294</span>,<span style="color:#ae81ff">0x72f7</span>,<span style="color:#ae81ff">0x62d6</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x9339</span>,<span style="color:#ae81ff">0x8318</span>,<span style="color:#ae81ff">0xb37b</span>,<span style="color:#ae81ff">0xa35a</span>,<span style="color:#ae81ff">0xd3bd</span>,<span style="color:#ae81ff">0xc39c</span>,<span style="color:#ae81ff">0xf3ff</span>,<span style="color:#ae81ff">0xe3de</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x2462</span>,<span style="color:#ae81ff">0x3443</span>,<span style="color:#ae81ff">0x0420</span>,<span style="color:#ae81ff">0x1401</span>,<span style="color:#ae81ff">0x64e6</span>,<span style="color:#ae81ff">0x74c7</span>,<span style="color:#ae81ff">0x44a4</span>,<span style="color:#ae81ff">0x5485</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xa56a</span>,<span style="color:#ae81ff">0xb54b</span>,<span style="color:#ae81ff">0x8528</span>,<span style="color:#ae81ff">0x9509</span>,<span style="color:#ae81ff">0xe5ee</span>,<span style="color:#ae81ff">0xf5cf</span>,<span style="color:#ae81ff">0xc5ac</span>,<span style="color:#ae81ff">0xd58d</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x3653</span>,<span style="color:#ae81ff">0x2672</span>,<span style="color:#ae81ff">0x1611</span>,<span style="color:#ae81ff">0x0630</span>,<span style="color:#ae81ff">0x76d7</span>,<span style="color:#ae81ff">0x66f6</span>,<span style="color:#ae81ff">0x5695</span>,<span style="color:#ae81ff">0x46b4</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xb75b</span>,<span style="color:#ae81ff">0xa77a</span>,<span style="color:#ae81ff">0x9719</span>,<span style="color:#ae81ff">0x8738</span>,<span style="color:#ae81ff">0xf7df</span>,<span style="color:#ae81ff">0xe7fe</span>,<span style="color:#ae81ff">0xd79d</span>,<span style="color:#ae81ff">0xc7bc</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x48c4</span>,<span style="color:#ae81ff">0x58e5</span>,<span style="color:#ae81ff">0x6886</span>,<span style="color:#ae81ff">0x78a7</span>,<span style="color:#ae81ff">0x0840</span>,<span style="color:#ae81ff">0x1861</span>,<span style="color:#ae81ff">0x2802</span>,<span style="color:#ae81ff">0x3823</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xc9cc</span>,<span style="color:#ae81ff">0xd9ed</span>,<span style="color:#ae81ff">0xe98e</span>,<span style="color:#ae81ff">0xf9af</span>,<span style="color:#ae81ff">0x8948</span>,<span style="color:#ae81ff">0x9969</span>,<span style="color:#ae81ff">0xa90a</span>,<span style="color:#ae81ff">0xb92b</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x5af5</span>,<span style="color:#ae81ff">0x4ad4</span>,<span style="color:#ae81ff">0x7ab7</span>,<span style="color:#ae81ff">0x6a96</span>,<span style="color:#ae81ff">0x1a71</span>,<span style="color:#ae81ff">0x0a50</span>,<span style="color:#ae81ff">0x3a33</span>,<span style="color:#ae81ff">0x2a12</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xdbfd</span>,<span style="color:#ae81ff">0xcbdc</span>,<span style="color:#ae81ff">0xfbbf</span>,<span style="color:#ae81ff">0xeb9e</span>,<span style="color:#ae81ff">0x9b79</span>,<span style="color:#ae81ff">0x8b58</span>,<span style="color:#ae81ff">0xbb3b</span>,<span style="color:#ae81ff">0xab1a</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x6ca6</span>,<span style="color:#ae81ff">0x7c87</span>,<span style="color:#ae81ff">0x4ce4</span>,<span style="color:#ae81ff">0x5cc5</span>,<span style="color:#ae81ff">0x2c22</span>,<span style="color:#ae81ff">0x3c03</span>,<span style="color:#ae81ff">0x0c60</span>,<span style="color:#ae81ff">0x1c41</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xedae</span>,<span style="color:#ae81ff">0xfd8f</span>,<span style="color:#ae81ff">0xcdec</span>,<span style="color:#ae81ff">0xddcd</span>,<span style="color:#ae81ff">0xad2a</span>,<span style="color:#ae81ff">0xbd0b</span>,<span style="color:#ae81ff">0x8d68</span>,<span style="color:#ae81ff">0x9d49</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x7e97</span>,<span style="color:#ae81ff">0x6eb6</span>,<span style="color:#ae81ff">0x5ed5</span>,<span style="color:#ae81ff">0x4ef4</span>,<span style="color:#ae81ff">0x3e13</span>,<span style="color:#ae81ff">0x2e32</span>,<span style="color:#ae81ff">0x1e51</span>,<span style="color:#ae81ff">0x0e70</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xff9f</span>,<span style="color:#ae81ff">0xefbe</span>,<span style="color:#ae81ff">0xdfdd</span>,<span style="color:#ae81ff">0xcffc</span>,<span style="color:#ae81ff">0xbf1b</span>,<span style="color:#ae81ff">0xaf3a</span>,<span style="color:#ae81ff">0x9f59</span>,<span style="color:#ae81ff">0x8f78</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x9188</span>,<span style="color:#ae81ff">0x81a9</span>,<span style="color:#ae81ff">0xb1ca</span>,<span style="color:#ae81ff">0xa1eb</span>,<span style="color:#ae81ff">0xd10c</span>,<span style="color:#ae81ff">0xc12d</span>,<span style="color:#ae81ff">0xf14e</span>,<span style="color:#ae81ff">0xe16f</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x1080</span>,<span style="color:#ae81ff">0x00a1</span>,<span style="color:#ae81ff">0x30c2</span>,<span style="color:#ae81ff">0x20e3</span>,<span style="color:#ae81ff">0x5004</span>,<span style="color:#ae81ff">0x4025</span>,<span style="color:#ae81ff">0x7046</span>,<span style="color:#ae81ff">0x6067</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x83b9</span>,<span style="color:#ae81ff">0x9398</span>,<span style="color:#ae81ff">0xa3fb</span>,<span style="color:#ae81ff">0xb3da</span>,<span style="color:#ae81ff">0xc33d</span>,<span style="color:#ae81ff">0xd31c</span>,<span style="color:#ae81ff">0xe37f</span>,<span style="color:#ae81ff">0xf35e</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x02b1</span>,<span style="color:#ae81ff">0x1290</span>,<span style="color:#ae81ff">0x22f3</span>,<span style="color:#ae81ff">0x32d2</span>,<span style="color:#ae81ff">0x4235</span>,<span style="color:#ae81ff">0x5214</span>,<span style="color:#ae81ff">0x6277</span>,<span style="color:#ae81ff">0x7256</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xb5ea</span>,<span style="color:#ae81ff">0xa5cb</span>,<span style="color:#ae81ff">0x95a8</span>,<span style="color:#ae81ff">0x8589</span>,<span style="color:#ae81ff">0xf56e</span>,<span style="color:#ae81ff">0xe54f</span>,<span style="color:#ae81ff">0xd52c</span>,<span style="color:#ae81ff">0xc50d</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x34e2</span>,<span style="color:#ae81ff">0x24c3</span>,<span style="color:#ae81ff">0x14a0</span>,<span style="color:#ae81ff">0x0481</span>,<span style="color:#ae81ff">0x7466</span>,<span style="color:#ae81ff">0x6447</span>,<span style="color:#ae81ff">0x5424</span>,<span style="color:#ae81ff">0x4405</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xa7db</span>,<span style="color:#ae81ff">0xb7fa</span>,<span style="color:#ae81ff">0x8799</span>,<span style="color:#ae81ff">0x97b8</span>,<span style="color:#ae81ff">0xe75f</span>,<span style="color:#ae81ff">0xf77e</span>,<span style="color:#ae81ff">0xc71d</span>,<span style="color:#ae81ff">0xd73c</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x26d3</span>,<span style="color:#ae81ff">0x36f2</span>,<span style="color:#ae81ff">0x0691</span>,<span style="color:#ae81ff">0x16b0</span>,<span style="color:#ae81ff">0x6657</span>,<span style="color:#ae81ff">0x7676</span>,<span style="color:#ae81ff">0x4615</span>,<span style="color:#ae81ff">0x5634</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xd94c</span>,<span style="color:#ae81ff">0xc96d</span>,<span style="color:#ae81ff">0xf90e</span>,<span style="color:#ae81ff">0xe92f</span>,<span style="color:#ae81ff">0x99c8</span>,<span style="color:#ae81ff">0x89e9</span>,<span style="color:#ae81ff">0xb98a</span>,<span style="color:#ae81ff">0xa9ab</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x5844</span>,<span style="color:#ae81ff">0x4865</span>,<span style="color:#ae81ff">0x7806</span>,<span style="color:#ae81ff">0x6827</span>,<span style="color:#ae81ff">0x18c0</span>,<span style="color:#ae81ff">0x08e1</span>,<span style="color:#ae81ff">0x3882</span>,<span style="color:#ae81ff">0x28a3</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xcb7d</span>,<span style="color:#ae81ff">0xdb5c</span>,<span style="color:#ae81ff">0xeb3f</span>,<span style="color:#ae81ff">0xfb1e</span>,<span style="color:#ae81ff">0x8bf9</span>,<span style="color:#ae81ff">0x9bd8</span>,<span style="color:#ae81ff">0xabbb</span>,<span style="color:#ae81ff">0xbb9a</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x4a75</span>,<span style="color:#ae81ff">0x5a54</span>,<span style="color:#ae81ff">0x6a37</span>,<span style="color:#ae81ff">0x7a16</span>,<span style="color:#ae81ff">0x0af1</span>,<span style="color:#ae81ff">0x1ad0</span>,<span style="color:#ae81ff">0x2ab3</span>,<span style="color:#ae81ff">0x3a92</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xfd2e</span>,<span style="color:#ae81ff">0xed0f</span>,<span style="color:#ae81ff">0xdd6c</span>,<span style="color:#ae81ff">0xcd4d</span>,<span style="color:#ae81ff">0xbdaa</span>,<span style="color:#ae81ff">0xad8b</span>,<span style="color:#ae81ff">0x9de8</span>,<span style="color:#ae81ff">0x8dc9</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x7c26</span>,<span style="color:#ae81ff">0x6c07</span>,<span style="color:#ae81ff">0x5c64</span>,<span style="color:#ae81ff">0x4c45</span>,<span style="color:#ae81ff">0x3ca2</span>,<span style="color:#ae81ff">0x2c83</span>,<span style="color:#ae81ff">0x1ce0</span>,<span style="color:#ae81ff">0x0cc1</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0xef1f</span>,<span style="color:#ae81ff">0xff3e</span>,<span style="color:#ae81ff">0xcf5d</span>,<span style="color:#ae81ff">0xdf7c</span>,<span style="color:#ae81ff">0xaf9b</span>,<span style="color:#ae81ff">0xbfba</span>,<span style="color:#ae81ff">0x8fd9</span>,<span style="color:#ae81ff">0x9ff8</span>,
</span></span><span style="display:flex;"><span>	<span style="color:#ae81ff">0x6e17</span>,<span style="color:#ae81ff">0x7e36</span>,<span style="color:#ae81ff">0x4e55</span>,<span style="color:#ae81ff">0x5e74</span>,<span style="color:#ae81ff">0x2e93</span>,<span style="color:#ae81ff">0x3eb2</span>,<span style="color:#ae81ff">0x0ed1</span>,<span style="color:#ae81ff">0x1ef0</span>
</span></span><span style="display:flex;"><span>};
</span></span><span style="display:flex;"><span>  
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">short</span> <span style="color:#a6e22e">crc16_ccitt_check</span>(<span style="color:#66d9ef">const</span> <span style="color:#66d9ef">void</span> <span style="color:#f92672">*</span>buf, <span style="color:#66d9ef">int</span> len)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">register</span> <span style="color:#66d9ef">int</span> counter;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">register</span> <span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">short</span> crc <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">char</span> <span style="color:#f92672">*</span> data <span style="color:#f92672">=</span> (<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">char</span> <span style="color:#f92672">*</span>)buf;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span>( counter <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; counter <span style="color:#f92672">&lt;</span> len; counter<span style="color:#f92672">++</span>)
</span></span><span style="display:flex;"><span>		crc <span style="color:#f92672">=</span> (crc<span style="color:#f92672">&lt;&lt;</span><span style="color:#ae81ff">8</span>) <span style="color:#f92672">^</span> crc16tab[((crc<span style="color:#f92672">&gt;&gt;</span><span style="color:#ae81ff">8</span>) <span style="color:#f92672">^</span> <span style="color:#f92672">*</span>data<span style="color:#f92672">++</span>) <span style="color:#f92672">&amp;</span> <span style="color:#ae81ff">0x00FF</span>];
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> crc;
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>
<h3 id="crc32">
  <a class="Heading-link u-clickable" href="/post/xiaomi_xmodem_crc32_crc16_crc8/#crc32">crc32</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-c" data-lang="c"><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdio.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e">#include</span> <span style="color:#75715e">&lt;stdint.h&gt;</span><span style="color:#75715e">
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">uint32_t</span> <span style="color:#a6e22e">soft_crc32</span>(<span style="color:#66d9ef">const</span> <span style="color:#66d9ef">void</span> <span style="color:#f92672">*</span>__data, <span style="color:#66d9ef">int</span> data_size, <span style="color:#66d9ef">uint32_t</span> crc);
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">soft_crc32_init</span>(<span style="color:#66d9ef">void</span>);
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#define TABLE_SIZE 256
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">static</span> <span style="color:#66d9ef">uint32_t</span> crc_table[TABLE_SIZE];
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">static</span> <span style="color:#66d9ef">const</span> <span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">int</span> rcrc32 <span style="color:#f92672">=</span> <span style="color:#ae81ff">0xEDB88320</span>;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">void</span> <span style="color:#a6e22e">soft_crc32_init</span>(<span style="color:#66d9ef">void</span>)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">int</span> crc <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">char</span> i;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">int</span> j;
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">for</span> (j <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>; j <span style="color:#f92672">&lt;</span> TABLE_SIZE; j<span style="color:#f92672">++</span>) {
</span></span><span style="display:flex;"><span>		crc <span style="color:#f92672">=</span> <span style="color:#ae81ff">0</span>;
</span></span><span style="display:flex;"><span>		<span style="color:#66d9ef">for</span> (i <span style="color:#f92672">=</span> <span style="color:#ae81ff">0x01</span>; i <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0x00</span>; i <span style="color:#f92672">&lt;&lt;=</span> <span style="color:#ae81ff">1</span>) {
</span></span><span style="display:flex;"><span>			<span style="color:#66d9ef">if</span> ((crc <span style="color:#f92672">&amp;</span> <span style="color:#ae81ff">0x00000001</span>) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span>) {
</span></span><span style="display:flex;"><span>				crc <span style="color:#f92672">&gt;&gt;=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>				crc <span style="color:#f92672">^=</span> rcrc32;
</span></span><span style="display:flex;"><span>			} <span style="color:#66d9ef">else</span> {
</span></span><span style="display:flex;"><span>				crc <span style="color:#f92672">&gt;&gt;=</span> <span style="color:#ae81ff">1</span>;
</span></span><span style="display:flex;"><span>			}
</span></span><span style="display:flex;"><span>			<span style="color:#66d9ef">if</span> ((j <span style="color:#f92672">&amp;</span> i) <span style="color:#f92672">!=</span> <span style="color:#ae81ff">0</span>)
</span></span><span style="display:flex;"><span>				crc <span style="color:#f92672">^=</span> rcrc32;
</span></span><span style="display:flex;"><span>		}
</span></span><span style="display:flex;"><span>		crc_table[j] <span style="color:#f92672">=</span> crc;
</span></span><span style="display:flex;"><span>		<span style="color:#75715e">//printf(&#34;%08x\r\n&#34;, crc);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">uint32_t</span> <span style="color:#a6e22e">soft_crc32</span>(<span style="color:#66d9ef">const</span> <span style="color:#66d9ef">void</span> <span style="color:#f92672">*</span>__data, <span style="color:#66d9ef">int</span> data_size, <span style="color:#66d9ef">uint32_t</span> crc)
</span></span><span style="display:flex;"><span>{
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">const</span> <span style="color:#66d9ef">uint8_t</span> <span style="color:#f92672">*</span>data <span style="color:#f92672">=</span>(<span style="color:#66d9ef">const</span> <span style="color:#66d9ef">uint8_t</span> <span style="color:#f92672">*</span>) __data;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">int</span> result <span style="color:#f92672">=</span> crc;
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">char</span> crc_H8;
</span></span><span style="display:flex;"><span>	<span style="color:#a6e22e">soft_crc32_init</span>();
</span></span><span style="display:flex;"><span>	<span style="color:#75715e">//printf(&#34;soft_crc32 data_size= %d\r\n&#34;, data_size);
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span>	<span style="color:#66d9ef">while</span> (data_size<span style="color:#f92672">--</span>) {
</span></span><span style="display:flex;"><span>		crc_H8 <span style="color:#f92672">=</span> (<span style="color:#66d9ef">unsigned</span> <span style="color:#66d9ef">char</span>)(result <span style="color:#f92672">&amp;</span> <span style="color:#ae81ff">0x000000FF</span>);
</span></span><span style="display:flex;"><span>		result <span style="color:#f92672">&gt;&gt;=</span> <span style="color:#ae81ff">8</span>;
</span></span><span style="display:flex;"><span>		result <span style="color:#f92672">^=</span> crc_table[crc_H8 <span style="color:#f92672">^</span> (<span style="color:#f92672">*</span>data)];
</span></span><span style="display:flex;"><span>		data<span style="color:#f92672">++</span>;
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>	<span style="color:#66d9ef">return</span> result;
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>

      
      


  

  

  





    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/kotlin_nsdata_to_bytearray/" rel="bookmark">Kotlin Native 开发 NSData转ByteArray</a>
  </h2>
  
    <time datetime="2023-04-20T13:02:36&#43;08:00">20 April, 2023</time>
  
</header>

      
        <h3 id="string转nsdata">
  <a class="Heading-link u-clickable" href="/post/kotlin_nsdata_to_bytearray/#string%e8%bd%acnsdata">String转NSData</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-kotlin" data-lang="kotlin"><span style="display:flex;"><span><span style="color:#66d9ef">val</span> nsdata = (<span style="color:#e6db74">&#34;Hello&#34;</span> <span style="color:#66d9ef">as</span> NSString).dataUsingEncoding(NSUTF8StringEncoding)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">val</span> hello = <span style="color:#e6db74">&#34;Hello&#34;</span>
</span></span><span style="display:flex;"><span><span style="color:#a6e22e">NSData</span>.create(bytes = hello.cstr.ptr, length = hello.length)</span></span></code></pre></div>
<h3 id="nsdata转bytearray">
  <a class="Heading-link u-clickable" href="/post/kotlin_nsdata_to_bytearray/#nsdata%e8%bd%acbytearray">NSData转ByteArray</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-kotlin" data-lang="kotlin"><span style="display:flex;"><span><span style="color:#75715e">// 实现1
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">if</span> (nsdata <span style="color:#f92672">!=</span> <span style="color:#66d9ef">null</span>) {
</span></span><span style="display:flex;"><span>    <span style="color:#66d9ef">val</span> byteArray = ByteArray(nsdata.length.toInt())
</span></span><span style="display:flex;"><span>    byteArray.usePinned {
</span></span><span style="display:flex;"><span>        memcpy(<span style="color:#66d9ef">it</span>.addressOf(<span style="color:#ae81ff">0</span>), nsdata.bytes, nsdata.length)
</span></span><span style="display:flex;"><span>    }
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 实现2
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">val</span> byteArray = ByteArray(<span style="color:#66d9ef">data</span>.length.toInt())
</span></span><span style="display:flex;"><span>byteArray.apply {
</span></span><span style="display:flex;"><span>  usePinned {
</span></span><span style="display:flex;"><span>  		memcpy(<span style="color:#66d9ef">it</span>.addressOf(<span style="color:#ae81ff">0</span>), <span style="color:#66d9ef">data</span>.bytes, <span style="color:#66d9ef">data</span>.length)
</span></span><span style="display:flex;"><span>	}
</span></span><span style="display:flex;"><span>}
</span></span><span style="display:flex;"><span><span style="color:#75715e">// 实现3
</span></span></span><span style="display:flex;"><span><span style="color:#75715e"></span><span style="color:#66d9ef">val</span> byteArray = <span style="color:#66d9ef">data</span>.bytes<span style="color:#f92672">?.</span>readBytes(<span style="color:#66d9ef">data</span>.length.toInt())</span></span></code></pre></div>
<h3 id="bytearray-转-nsdata">
  <a class="Heading-link u-clickable" href="/post/kotlin_nsdata_to_bytearray/#bytearray-%e8%bd%ac-nsdata">ByteArray 转 NSData</a>
</h3>

<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-kotlin" data-lang="kotlin"><span style="display:flex;"><span><span style="color:#66d9ef">var</span> nsdata: NSData
</span></span><span style="display:flex;"><span>byteArray<span style="color:#f92672">?.</span>usePinned {
</span></span><span style="display:flex;"><span>    nsdata = <span style="color:#a6e22e">NSData</span>.dataWithBytes(<span style="color:#66d9ef">it</span>.addressOf(<span style="color:#ae81ff">0</span>), <span style="color:#66d9ef">it</span>.<span style="color:#66d9ef">get</span>().size.toULong())
</span></span><span style="display:flex;"><span>}</span></span></code></pre></div>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/kotlin/" rel="tag">Kotlin</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/native/" rel="tag">Native</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/nsdata/" rel="tag">NSData</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/bytearray/" rel="tag">ByteArray</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/string/" rel="tag">String</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/string%E8%BD%ACnsdata/" rel="tag">String转NSData</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/nsdata%E8%BD%ACbytearray/" rel="tag">NSData转ByteArray</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/bytearray-%E8%BD%AC-nsdata/" rel="tag">ByteArray 转 NSData</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  
    <article>
      <header class="Heading">
  <h2 class="Heading-title">
    <a class="Heading-link u-clickable" href="/post/golang_value_reference_type/" rel="bookmark">golang中的值类型和引用类型</a>
  </h2>
  
    <time datetime="2023-04-19T00:48:08&#43;08:00">19 April, 2023</time>
  
</header>

      
        <ul>
<li>值类型分别有：int系列、float系列、bool、string、数组和结构体</li>
<li>引用类型有：指针、slice切片、管道channel、接口interface、map、函数等</li>
</ul>
<p>值类型的特点是：变量直接存储值，内存通常在栈中分配</p>
<p>引用类型的特点是：变量存储的是一个地址，这个地址对应的空间里才是真正存储的值，内存通常在堆中分配</p>
<p>对interface，map，函数 类型最好不要使用指针定义或取地址操作</p>

      
      


  

  

  





  <footer>
    
      
    
      
    
      
        <ul class="Tags">
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/go/" rel="tag">go</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%80%BC%E7%B1%BB%E5%9E%8B/" rel="tag">值类型</a>
            </li>
          
            <li class="Tags-item u-background">
              <a class="Tags-link u-clickable" href="/tags/%E5%BC%95%E7%94%A8%E7%B1%BB%E5%9E%8B/" rel="tag">引用类型</a>
            </li>
          
        </ul>
      
    
  </footer>

    </article>
    
      <div class="Divider"></div>
    
  

  
  <nav>
    
      <a class="Pagination u-clickable" href="/page/2/" rel="prev">« Previous</a>
    
    
  </nav>




      </div>
    </div>
  </main>
  
  <footer class="Footer">
    <div class="u-wrapper">
      <div class="u-padding">
        开发的乐趣在于相通而不同
      </div>
    </div>
  </footer>


  <script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?d08a2ad355cb7a065a41be081b43297e";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>

</body>

</html>
